﻿using MediatR;
using W.Warehouse.Api.Application.Command.WarehouseCommand;
using W.Warehouse.Api.Application.Command.WarehouseTypeCommand;
using W.Warehouse.Api.Application.CommandHandlder.WarehouseTypeCommandHandlder;
using W.Warehouse.Domain;
using W.Warehouse.Repository.Interface;

namespace W.Warehouse.Api.Application.CommandHandlder.WarehouseCommandHandlder
{
    public class WarehouseUpdateCommandHandlder : IRequestHandler<WarehouseUpdateCommand, ApiResult<int>>
    {
        private readonly ILogger<WarehouseUpdateCommandHandlder> _logger;
        private readonly IWarehouseInfoRepository _warehouseInfoRepository;
        public WarehouseUpdateCommandHandlder(IWarehouseInfoRepository warehouseInfoRepository, ILogger<WarehouseUpdateCommandHandlder> logger)
        {
            _warehouseInfoRepository = warehouseInfoRepository;
            _logger = logger;
        }
        public async Task<ApiResult<int>> Handle(WarehouseUpdateCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                _logger.LogInformation("修改仓库管理成功");
                request.WhetherDefault = false;
                request.WhetherDisable = false;
                request.IsDelete = false;
                request.UpdateTime = DateTime.Now;
                var warehouseInfo = new WarehouseInfo() { WarehouseId = request.WarehouseId, WarehouseNo = request.WarehouseNo, DepartmentId = request.DepartmentId, WarehouseName = request.WarehouseName, WarehouseTypeId = request.WarehouseTypeId, Function = request.Function, LeaseDate = request.LeaseDate, WarehouseArea = request.WarehouseArea, WarehouseAddress = request.WarehouseAddress, WarehouseContact = request.WarehouseContact, WarehousePhone = request.WarehousePhone, WhetherDisable = request.WhetherDisable, WhetherDefault = request.WhetherDefault, office_id = request.office_id, user_id = request.user_id, CreateName = request.CreateName, CreateTime = request.CreateTime, UpdateName = request.UpdateName, UpdateTime = request.UpdateTime, DeleteName = request.DeleteName, DeleteTime = request.DeleteTime, IsDelete = request.IsDelete };
                result.Code = await _warehouseInfoRepository.UpdateAsync(warehouseInfo);
            }
            catch (Exception el)
            {
                result.Code = -1;
                result.Message = el.Message;
                _logger.LogError(el, el.Message);
                throw;
            }
            return result;
        }
    }
}
